package com.gxp.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxp.common.annotation.Audit;
import com.gxp.common.config.ServerConfig;
import com.gxp.common.core.controller.BaseController;
import com.gxp.common.core.domain.AjaxResult;
import com.gxp.common.core.page.TableDataInfo;
import com.gxp.common.enums.BusinessAction;
import com.gxp.common.enums.BusinessTitle;
import com.gxp.common.utils.DateUtils;
import com.gxp.system.domain.ZknjExpTemplate;
import com.gxp.system.domain.ZknjMethod;
import com.gxp.system.domain.ZknjService;
import com.gxp.system.domain.dto.template.TemplateDto;
import com.gxp.system.service.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.XML;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 方法Controller
 * 
 * @author zh
 * @date 2024-07-19
 */
@Controller
@RequestMapping("/system/method")
public class ZknjMethodController extends BaseController
{
    private String prefix = "system/project/method";

    @Autowired
    private IZknjMethodService zknjMethodService;
    @Autowired
    private IZknjExpTemplateService expTemplateService;
    @Autowired
    private IZknjServiceService zknjServiceService;
    @Autowired
    private IZkSolventService solventService;
    @Autowired
    private IZkInstrumentService instrumentService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IZknjModSolvService modSolvService;

    @RequiresPermissions("system:method:view")
    @GetMapping()
    public String method()
    {
        return prefix + "/method";
    }

    /**
     * 查询方法列表
     */
    @RequiresPermissions("system:method:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZknjMethod zknjMethod)
    {
        startPage();
        List<ZknjMethod> list = zknjMethodService.selectZknjMethodList(zknjMethod);
        return getDataTable(list);
    }

    /**
     * 新增方法
     */
    @GetMapping("/add/{serId}")
    public String add(@PathVariable("serId") Long serId, ModelMap mmap)
    {
        Long instId = zknjServiceService.selectZknjServiceBySerId(serId).getInstId();
        //模板、溶剂信息
        ZknjExpTemplate expTemplate = new ZknjExpTemplate();
        expTemplate.setInstId(instId);
        List<ZknjExpTemplate> templates = expTemplateService.selectZknjExpTemplateList(expTemplate);
        mmap.addAttribute("templates", templates);
        mmap.addAttribute("serId", serId);
        return prefix + "/add";
    }

    /**
     * 新增保存方法
     */
    @RequiresPermissions("system:method:add")
    @Audit(businessTitle = BusinessTitle.METHOD, businessAction = BusinessAction.CREATED, description = "新增方法")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZknjMethod zknjMethod)
    {
        zknjMethod.setCreateBy(getLoginName());
        int row = zknjMethodService.insertZknjMethod(zknjMethod);
        return toAjax(row, zknjMethod.getModId());
    }

    /**
     * 修改方法
     */
    @RequiresPermissions("system:method:edit")
    @GetMapping("/edit/{modId}")
    public String edit(@PathVariable("modId") Long modId, ModelMap mmap)
    {
        ZknjMethod zknjMethod = zknjMethodService.selectZknjMethodByModId(modId);
        ZknjService zknjService = zknjServiceService.selectZknjServiceBySerId(zknjMethod.getSerId());
        ZknjExpTemplate expTemplate = new ZknjExpTemplate();
        expTemplate.setInstId(zknjService.getInstId());
        List<ZknjExpTemplate> templates = expTemplateService.selectZknjExpTemplateList(expTemplate);
        mmap.put("zknjMethod", zknjMethod);
        mmap.put("templates", templates);
        return prefix + "/edit";
    }

    /**
     * 修改保存方法
     */
    @RequiresPermissions("system:method:edit")
    @Audit(businessTitle = BusinessTitle.METHOD, businessAction = BusinessAction.UPDATED, description = "修改方法")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZknjMethod zknjMethod)
    {
        zknjMethod.setUpdateBy(getLoginName());
        return toAjax(zknjMethodService.updateZknjMethod(zknjMethod), zknjMethod.getModId());
    }

    /**
     * 删除方法
     */
    @RequiresPermissions("system:method:remove")
    @Audit(businessTitle = BusinessTitle.METHOD, businessAction = BusinessAction.DELETED, description = "删除方法")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zknjMethodService.deleteZknjMethodByModIds(ids));
    }

    /**
     * 选中Template事件
     */
    @PostMapping("/selectTemplate")
    @ResponseBody
    public AjaxResult selectTemplate(@Param("temId") Long temId){
        if(temId == null || temId == 0){
            return AjaxResult.success();
        }
        ZknjExpTemplate template = expTemplateService.selectZknjExpTemplateByTemId(temId);
        String para = template.getTemPara();
        if(para == null || para.isEmpty()) {
            return AjaxResult.error("The template is not available: " + template.getTemName());
        }
        //解析xml
        //xmlString  ->  json.JSONObject
        org.json.JSONObject jsonObject = XML.toJSONObject(para);
        //json.JSONObject  ->  jsonString  ->  JSONObject(fastjson)
        JSONObject paramJson = JSONObject.parseObject(jsonObject.toString());
        //JSONObject(fastjson)  ->  java对象
        TemplateDto templateDto = paramJson.toJavaObject(TemplateDto.class);

        return AjaxResult.success(templateDto);
    }

    /**
     * 方法审核
     */
    @RequiresPermissions("system:method:approve")
    @GetMapping("/approve")
    public String approve(ZknjMethod zknjMethod, ModelMap mmap){
        ZknjMethod method = zknjMethodService.selectZknjMethodByModId(zknjMethod.getModId());
        mmap.addAttribute("zknjMethod", method);
        return prefix + "/approve";
    }

    /**
     * 方法审核
     * @param zknjMethod
     * @return
     */
    @Audit(businessTitle = BusinessTitle.METHOD, businessAction = BusinessAction.APPROVED, description = "方法审核")
    @RequiresPermissions("system:method:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ZknjMethod zknjMethod){
        zknjMethod.setApproveBy(getLoginName());
        zknjMethod.setApproveTime(DateUtils.getNowDate());
        return toAjax(zknjMethodService.approve(zknjMethod), zknjMethod.getModId());
    }

    /**
     * 校验方法名
     */
    @PostMapping("/checkModNameUnique")
    @ResponseBody
    public boolean checkModNameUnique(ZknjMethod zknjMethod)
    {
        return zknjMethodService.checkModNameUnique(zknjMethod);
    }
}
